package com.quasar.hpatient.view.personal.collection;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.CarouselData;
import com.quasar.hpatient.model.medicalmodel.CollectionListData;
import com.quasar.hpatient.model.medicalmodel.ConsultationBean;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.onInterface.OnConsultationListener;
import com.quasar.hpatient.presenter.RecommendListPresenter;
import com.quasar.hpatient.utils.GlideImageLoader;
import com.quasar.hpatient.view.WebActivity_;
import com.quasar.hpatient.view.adapter.RecommendListAdapter;
import com.vise.log.ViseLog;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/10/23 0023
 * View:咨询
 */

@EFragment(R.layout.recommend_fragment)
public class RecommendFragment extends BaseFragment implements OnConsultationListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private RecommendListPresenter recommendListPresenter;
    int page = 1;
    public RecommendListAdapter recommendListAdapter;
    private int delayMillis = 1000;//设置刷新等待的时间
    //判断下一页是否存在数据
    private boolean onLoadMore = false;
    List<String> list = new ArrayList<>();

    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;

    @ViewById(R.id.banner)
    Banner banner;

    @AfterViews
    void init() {
        ViseLog.d("enter");
        initView();
        initData();
        initAdapter();
    }

    @Override
    public void initView() {
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void initData() {
        recommendListPresenter = new RecommendListPresenter(this);
        recommendListPresenter.GetCarouselInformationsData("1");//轮播分页1
    }

    //初始化adapter布局
    public void initAdapter() {
        recommendListAdapter = new RecommendListAdapter();
        recommendListAdapter.setOnLoadMoreListener(RecommendFragment.this, rv_list);
        recommendListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        rv_list.setAdapter(recommendListAdapter);
        recommendListPresenter.RecommendList(page + "");
        //item点击
        rv_list.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                ViseLog.d("onSimpleItemClick" + recommendListAdapter.getItem(position).getId());
                //咨询详情
                CounsultWebViewActivity_.intent(getActivity()).recordId(recommendListAdapter.getItem(position).getId() + "").start();
            }
        });
    }

    @Override
    public void onConsultationListSuccess(AnotherResult<ConsultationBean> data) {
        //判断page是否是下拉还是上拉
        if (page == 1) {
            recommendListAdapter.setNewData(data.getList());
            onLoadMore = false;
        } else {
            recommendListAdapter.addData(data.getList());
            if (data.getList().size() == 0) {
                onLoadMore = true;
            }
        }
    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onConsultationRefreshUI() {
        recommendListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCarouselSuccess(final AnotherResult<CarouselData> dataAnotherResult) {
        list.clear();
        if (dataAnotherResult.getList() != null) {
            if (dataAnotherResult.getList().size() > 0) {
                for (int i = 0; i < dataAnotherResult.getList().size(); i++) {
                    list.add(RetrofitConnection.PHOTOURL + dataAnotherResult.getList().get(i).getCover());
                }

                ViseLog.d("点击了咨询轮播");
                banner.setImages(list)
                        .setImageLoader(new GlideImageLoader())
                        .setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int position) {

                                ViseLog.d("点击了咨询轮播");

                                if (list.size() != 0) {
//                                WebActivity_.intent(getActivity()).url(dataAnotherResult.getList().get(position).getId() + "").start();
                                    CounsultWebViewActivity_.intent(getActivity()).recordId(dataAnotherResult.getList().get(position).getId() + "").start();
                                }
                            }
                        })
                        .start();
            }
        }
    }


    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        page = 1;
        recommendListPresenter.RecommendList(page + "");
        recommendListAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                recommendListAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;//下拉时page递增
        recommendListPresenter.RecommendList(page + "");
        rv_list.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当返回的数据为空时停止下拉刷新
                if (onLoadMore) {
                    recommendListAdapter.loadMoreEnd();
                } else {
                    recommendListAdapter.loadMoreComplete();
                    recommendListAdapter.notifyDataSetChanged();
                }
            }

        }, delayMillis);
    }

    @Override
    public void onMyConsultationListSuccess(AnotherResult<CollectionListData> dataAnotherResult) {

    }

    @Override
    public void onNottoCollectSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onButtonCollectionSuccess(DataStateResult dataStateResult) {

    }

}
